home *** CD-ROM | disk | FTP | other *** search
- Path: news.ov.com!news
- From: glenn@ov.com (Fletcher.Glenn@ov.com)
- Newsgroups: comp.lang.c
- Subject: Re: A Very Simple Socket Question
- Date: 19 Jan 1996 19:02:11 GMT
- Organization: OpenVision
- Message-ID: <4doprj$9t8@spanky.pls.ov.com>
- References: <4dfgj8$6p9@nntp.ucs.ubc.ca>
- Reply-To: glenn@ov.com
- NNTP-Posting-Host: foghorn.pls.ov.com
-
- In article 6p9@nntp.ucs.ubc.ca, evil@unixg.ubc.ca (Peter Pan) writes:
- >Please help! I am a C beginner.
- >
- >======================================
- >Client:
- >
- >int sd, addrlen;
- >struct sockaddr_in svaddr;
- >
- >char *data;
- >data = "abcdef";
- >
- >..... /* skip */
- >
- >sendto(sd, data, strlen(data), 0, &svaddr, &addrlen);
- ^^^^^^^^^^^^
- This will not send the terminating null character, use "strlen(data) + 1".
-
- >
- >
- >==========================================
- >Server:
- >
- >char data;
- >data = (char*) malloc( 100*sizeof(char) );
- >
- >recvfrom(sd2, data, sizeof(data), 0, &claddr, &addrlen);
- ^^^^^^^^^^^^
- data is a char * with a size of 4. What you really want is 100.
- Even so, I expect you will block waiting for the remaining 93 chars.
- When you use these functions you should agree in advance about
- the transfer length, even if you have to precede the actual data
- with a message (of known length) containing information about the
- length of the data to follow.
-
- Fletcher.Glenn@ov.com
-
- >
- >==========================================
- >
- >Output:
- >
- >data sent: abcdef
- >data received: abcd
- >
- >==========================================
- >
- >Anyone knows why the data are different after transmittion.
- >
- >Thanks.
- >Leo
- >
-
-